package com.bmf.module.examination.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bmf.constant.AdminResultEnum;
import com.bmf.core.result.PageResponse;
import com.bmf.enums.QuestionTypeEnum;
import com.bmf.enums.StatisticalEnum;
import com.bmf.module.examination.vo.*;
import com.bmf.module.examination.entity.*;
import com.bmf.module.examination.mapper.TestPaperMapper;
import com.bmf.module.examination.service.*;
import com.bmf.module.rbac.entity.RbacUser;
import com.bmf.module.rbac.service.RbacUserService;
import com.bmf.module.testQuestions.entity.SmallQuestion;
import com.bmf.module.testQuestions.service.SmallQuestionService;
import com.bmf.module.testQuestions.vo.QuestionTestPaperResultVo;
import com.bmf.utils.BeanCopyUtils;
import com.bmf.utils.MyExcelExportUtil;
import com.bmf.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * (TestPaper)表服务实现类
 *
 * @author makejava
 * @since 2022-12-02 14:39:07
 */
@Service("testPaperService")
public class TestPaperServiceImpl extends ServiceImpl<TestPaperMapper, TestPaper> implements TestPaperService {


    @Autowired
    private SmallQuestionService smallQuestionService;

    @Autowired
    private RbacUserService rbacUserService;

    @Autowired
    private TestinfoService testinfoService;

    @Autowired
    private ExaminationService examinationService;



    @Override
    public ExaminationResultsCreateVo getTestPaper(Integer id,String studentName) {
        Testinfo testinfo = testinfoService.getById(id);
        if(testinfo == null){
            return null;
        }
        //1.判断是否还有考试次数
        LambdaQueryWrapper<TestPaper> wrapper = new LambdaQueryWrapper<>();
        if(studentName==null){
            wrapper.eq(TestPaper::getStudentId, SecurityUtils.getUserId());
        }else{
            wrapper.eq(TestPaper::getStudentName, studentName);
        }
        wrapper.eq(TestPaper::getTestinfoId, id);

        Integer frequency;
        TestPaper one = getOne(wrapper);
        if(one == null){
            frequency = testinfo.getFrequency();
        }else{
            // 没有考试次数
            if(one.getFrequency() == 0){
                AdminResultEnum.NO_TIMES.assertIsTrue(false);
            }
            frequency = one.getFrequency();
        }
        List<BigQuestionTestPaperResultVo> examination = examinationService.getExamination(testinfo.getExaminationId());

        if(testinfo.getExaminationId()==null||CollUtil.isEmpty(examination)){
            AdminResultEnum.NO_EXAM.assertIsTrue(false);
        }

        return new ExaminationResultsCreateVo(testinfo.getId(),testinfo.getExaminationId()
                ,frequency,examination,null,null,null,null);
    }


    @Override
    public void saveTest(ExaminationResultsCreateVo examinationResultsCreateVo) {
        TestPaper testPaper = new TestPaper();
        testPaper.setStudentId(SecurityUtils.getUserId())
                .setExaminationId(examinationResultsCreateVo.getExaminationId())
                .setTestinfoId(examinationResultsCreateVo.getTestinfoId())
                .setContent(JSONArray.toJSONString(examinationResultsCreateVo.getBigQuestionTestPaperResultVos()))
                .setStartTime(examinationResultsCreateVo.getStartTime())
                .setEndTime(examinationResultsCreateVo.getEndTime());
        save(testPaper);
    }

    @Override
    public ExaminationResultsCreateVo querySaveTest(Integer testinfoId) {
        LambdaQueryWrapper<TestPaper> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TestPaper::getTestinfoId, testinfoId).eq(TestPaper::getStudentId, SecurityUtils.getUserId());
        TestPaper one = getOne(wrapper);
        if(one==null){
            return null;
        }
        return new ExaminationResultsCreateVo(testinfoId,one.getExaminationId(),one.getFrequency(),JSONArray.parseArray(one.getContent(), BigQuestionTestPaperResultVo.class),null,null,null,null);
    }

    @Override
    public void submitTest(ExaminationResultsCreateVo examinationResultsCreateVo) {
        List<BigQuestionTestPaperResultVo> bigQuestionTestPaperResultVos = examinationResultsCreateVo.getBigQuestionTestPaperResultVos();
        if(CollUtil.isNotEmpty(bigQuestionTestPaperResultVos)){
            //取得所有题目的ids
            List<Integer> ids = bigQuestionTestPaperResultVos.stream().filter(s->CollUtil.isNotEmpty(s.getQuestionTestPaperResultVos())).flatMap(s -> s.getQuestionTestPaperResultVos().stream()).map(QuestionTestPaperResultVo::getId).collect(Collectors.toList());
            //查找出所有题目，包含答案
            List<SmallQuestion> smallQuestions = smallQuestionService.listByIds(ids);
            if(CollUtil.isNotEmpty(smallQuestions)){
                //题目转为map
                Map<Integer, SmallQuestion> map = smallQuestions.stream().collect(Collectors.toMap(SmallQuestion::getId, Function.identity()));
                bigQuestionTestPaperResultVos.forEach(s->{
                    //取得大题下所有题目
                    List<QuestionTestPaperResultVo> questionTestPaperResultVos = s.getQuestionTestPaperResultVos();
                    if(CollUtil.isNotEmpty(questionTestPaperResultVos)){
                        //给所有题目判分
                        questionTestPaperResultVos.forEach(x->{
                            //map查询题目
                            SmallQuestion smallQuestion = map.get(x.getId());
                            if(ObjectUtil.isNotEmpty(smallQuestion)){
                                //判分
                                getScore(x,smallQuestion);
                            }
                        });
                    }
                });
            }
        }
        //计算总分数
        double totalScore = bigQuestionTestPaperResultVos.stream().filter(s -> CollUtil.isNotEmpty(s.getQuestionTestPaperResultVos())).flatMap(s -> s.getQuestionTestPaperResultVos().stream()).filter(s->s.getGetScore()!=null).mapToDouble(QuestionTestPaperResultVo::getGetScore).sum();
        LambdaQueryWrapper<TestPaper> wrapper = new LambdaQueryWrapper<>();
        if(StrUtil.isNotBlank(examinationResultsCreateVo.getStudentName())){
            //未登录考试
            wrapper.eq(TestPaper::getStudentName, examinationResultsCreateVo.getStudentName());
        }else {
            wrapper.eq(TestPaper::getStudentId, SecurityUtils.getUserId());
        }
        wrapper.eq(TestPaper::getExaminationId, examinationResultsCreateVo.getExaminationId()).orderByDesc(TestPaper::getId).last("limit 1");
        TestPaper one = getOne(wrapper);
        TestPaper results = new TestPaper();
        results.setContent(JSONArray.toJSONString(bigQuestionTestPaperResultVos))
                .setExaminationId(examinationResultsCreateVo.getExaminationId())
                .setScore(totalScore)
                .setTestinfoId(examinationResultsCreateVo.getTestinfoId())
                .setStudentName(examinationResultsCreateVo.getStudentName())
                .setType(examinationResultsCreateVo.getType())
                .setEndTime(examinationResultsCreateVo.getEndTime())
                .setStartTime(examinationResultsCreateVo.getStartTime());
        if(StrUtil.isNotBlank(examinationResultsCreateVo.getStudentName())){
            results.setStudentName(examinationResultsCreateVo.getStudentName());
        }else{
            results.setStudentId(SecurityUtils.getUserId());
        }
        if(one == null){
            //没有考试，考试次数减去
            results.setFrequency(examinationResultsCreateVo.getFrequency()-1);
        }else{
            results.setFrequency(one.getFrequency()-1);
        }
        save(results);
    }

    private void getScore(QuestionTestPaperResultVo x, SmallQuestion smallQuestion) {
        List<String> testAnswer = x.getTestAnswer();
        //如果是单选，判断，多选直接判分，其他题型老师判卷
        if(x.getQuestionType()== QuestionTypeEnum.SINGCHOICE.getCode()
                ||x.getQuestionType()==QuestionTypeEnum.MULTIPLECHOICE.getCode()
                ||x.getQuestionType()==QuestionTypeEnum.JUDGE.getCode()){
//            List<String> list = Arrays.asList(StrUtil.split(smallQuestion.getAnswer(), ","));
            List<String> list = StrUtil.split(smallQuestion.getAnswer(), ",");
            //答案相等，给分
            if(testAnswer!=null&&testAnswer.stream().sorted().collect(Collectors.joining())
                    .equals(list.stream().sorted().collect(Collectors.joining()))){
                x.setGetScore(x.getScore());
            }else{
                x.setGetScore((double) 0);
            }
        }else{
            //其他题型直接赋予0分
            x.setGetScore((double) 0);
        }
    }

    @Override
    public List<TestPaperResultVo> selectTestList(Integer id) {
        LambdaQueryWrapper<TestPaper> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TestPaper::getTestinfoId, id);
        List<TestPaper> list = list(wrapper);
        if(CollUtil.isEmpty(list)){
            return null;
        }
        //查询学生信息
        List<Integer> userId = list.stream().map(TestPaper::getStudentId).collect(Collectors.toList());
        List<RbacUser> rbacUsers = rbacUserService.listByIds(userId);
        Map<Integer, RbacUser> map = rbacUsers.stream().collect(Collectors.toMap(RbacUser::getId, Function.identity()));
        //查询考试信息
        Testinfo byId = testinfoService.getById(id);
        return list.stream().map(s-> BeanCopyUtils.copyBean(s, TestPaperResultVo.class)).peek(s->{
            s.setStudentName(map.get(s.getStudentId())==null?s.getStudentName():map.get(s.getStudentId()).getName());
            if(byId!=null){
                s.setPassScore(byId.getPassScore());
            }
            assert byId != null;
            s.setPass(s.getScore()>=byId.getPassScore());
        }).collect(Collectors.toList());
    }

    @Override
    public void updateTest(TestPaperUpdateVo testPaperUpdateVo) {
        testPaperUpdateVo.setContent(JSON.toJSONString(testPaperUpdateVo.getBigQuestionTestPaperVoList()));
        List<BigQuestionTestPaperResultVo> bigQuestionTestPaperVoList = testPaperUpdateVo.getBigQuestionTestPaperVoList();
        TestPaper testPaper = BeanCopyUtils.copyBean(testPaperUpdateVo, TestPaper.class);
        testPaper.setStatus(0);
        if(CollUtil.isNotEmpty(bigQuestionTestPaperVoList)){
            //给所有题目分数相加，计算试卷总分
            double score = bigQuestionTestPaperVoList.stream().filter(s -> CollUtil.isNotEmpty(s.getQuestionTestPaperResultVos()))
                    .flatMap(s -> s.getQuestionTestPaperResultVos().stream()).filter(s -> s.getGetScore() != null).mapToDouble(QuestionTestPaperResultVo::getGetScore).sum();
            testPaper.setScore(score);
        }
        updateById(testPaper);
    }


    @Override
    public List<BigQuestionTestPaperResultVo> selectTestPaperInfo(Integer id) {
        TestPaper byId = getById(id);
        if(byId==null){
            return null;
        }
        List<BigQuestionTestPaperResultVo> list = JSONArray.parseArray(byId.getContent(), BigQuestionTestPaperResultVo.class);
        if(CollUtil.isEmpty(list)){
            return list;
        }
        //取得所有小题的ids
        List<Integer> ids = list.stream().filter(s -> CollUtil.isNotEmpty(s.getQuestionTestPaperResultVos()))
                .flatMap(s -> s.getQuestionTestPaperResultVos().stream()).map(QuestionTestPaperResultVo::getId)
                .collect(Collectors.toList());
        //根据小题ids去查找，并将正确答案封存对象中
        List<SmallQuestion> smallQuestions = smallQuestionService.listByIds(ids);
        if(CollUtil.isNotEmpty(smallQuestions)){
            Map<Integer, String> questionMap = smallQuestions.stream().collect(Collectors.toMap(SmallQuestion::getId, SmallQuestion::getAnswer));
            list.forEach(s->{
                List<QuestionTestPaperResultVo> questionTestPaperResultVos = s.getQuestionTestPaperResultVos();
                if(CollUtil.isNotEmpty(questionTestPaperResultVos)){
                    questionTestPaperResultVos.forEach(k-> k.setAnswer(questionMap.get(k.getId())));
                }
            });
        }
        return list;
    }

    @Override
    public List<TestPaper> getByTestinfoIds(List<Integer> testIds) {
        LambdaQueryWrapper<TestPaper> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(TestPaper::getTestinfoId, testIds);
        return list(wrapper);
    }

    @Override
    public List<AchievementResultVo> achievementStatistics(Integer testinId) {
        LambdaQueryWrapper<TestPaper> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TestPaper::getTestinfoId, testinId);
        List<TestPaper> list = list(wrapper);
        if(CollUtil.isEmpty(list)){
            return null;
        }
        //查询学生姓名
        List<Integer> studentIds = list.stream().map(TestPaper::getStudentId).filter(Objects::nonNull).collect(Collectors.toList());
        List<RbacUser> rbacUsers = null;
        if(CollUtil.isNotEmpty(studentIds)){
            rbacUsers = rbacUserService.listByIds(studentIds);
        }
        Map<Integer,String> nameMap = new HashMap<>();
        if(CollUtil.isNotEmpty(rbacUsers)){
            assert rbacUsers != null;
            nameMap = rbacUsers.stream().collect(Collectors.toMap(RbacUser::getId, RbacUser::getName));
        }
        //查询考试总次数
        Testinfo byId = testinfoService.getById(list.get(0).getTestinfoId());
        int count = 0;
        if(byId!=null){
            count = byId.getFrequency();
        }
        //todo 优化计算总分
        double countTotalScore = examinationService.countTotalScore(testinId);

        Map<Integer, String> finalMap = nameMap;
        int finalCount = count;
        List<AchievementResultVo> collect1 = list.stream().map(s -> {
            AchievementResultVo achievementResultVo = new AchievementResultVo();
            //1.姓名
            if (s.getStudentName() != null) {
                achievementResultVo.setStudentName(s.getStudentName());
            } else {
                achievementResultVo.setStudentName(finalMap.get(s.getStudentId()));
            }

            //2.参加次数
            achievementResultVo.setPartCount(finalCount - s.getFrequency());

            //3.总分/及格分
            assert byId != null;
            achievementResultVo.setFraction(countTotalScore + "/" + byId.getPassScore());

            //4.成绩
            achievementResultVo.setScore(s.getScore());

            //5.正确率
            List<BigQuestionTestPaperResultVo> list1 = JSONArray.parseArray(s.getContent(), BigQuestionTestPaperResultVo.class);
            if (CollUtil.isNotEmpty(list1)) {
                List<QuestionTestPaperResultVo> collect = list1.stream().filter(k -> CollUtil.isNotEmpty(k.getQuestionTestPaperResultVos()))
                        .flatMap(k -> k.getQuestionTestPaperResultVos().stream())
                        .collect(Collectors.toList());
                try{
                    long count1 = collect.stream().filter(k -> !(k.getGetScore().equals(k.getScore()))).count();
                    achievementResultVo.setAccuracy((double) count1 / collect.size());
                }catch (Exception e){
                    achievementResultVo.setAccuracy((double) 0);
                }

            } else {
                achievementResultVo.setAccuracy((double) 0);
            }

            //6.得分率
            achievementResultVo.setScoringRate(countTotalScore == 0 ? 0 : s.getScore() / countTotalScore);

            //7.是否及格
            achievementResultVo.setPass(s.getScore() >= byId.getPassScore());

            //8.状态
            achievementResultVo.setStatus(s.getStatus());

            return achievementResultVo;
        }).sorted(Comparator.comparing(AchievementResultVo::getScore)).collect(Collectors.toList());
        Collections.reverse(collect1);
        return collect1;
    }

    @Override
    public PageResponse<StatisticalResultVo> statisticalQuery(StatisticalQueryVo statisticalQueryVo) {
        if(CollUtil.isEmpty(statisticalQueryVo.getIds())){
            return null;
        }
        LambdaQueryWrapper<TestPaper> wrapper = new LambdaQueryWrapper<>();
        if(statisticalQueryVo.getType()==StatisticalEnum.PERSONNEL.getCode()){
            wrapper.in(TestPaper::getStudentId,statisticalQueryVo.getIds());
        }else{
            wrapper.in(TestPaper::getTestinfoId,statisticalQueryVo.getIds());
        }

        if(statisticalQueryVo.getEndTime()!=null){
            wrapper.le(TestPaper::getEndTime,statisticalQueryVo.getEndTime());
        }
        if(statisticalQueryVo.getStartTime()!=null){
            wrapper.ge(TestPaper::getEndTime,statisticalQueryVo.getStartTime());
        }
        wrapper.select(TestPaper::getTestinfoId,TestPaper::getExaminationId,TestPaper::getStudentId
                ,TestPaper::getScore,TestPaper::getType
                ,TestPaper::getStudentName,TestPaper::getId);
        List<TestPaper> list3 = list(wrapper);
        List<Integer> idList = new ArrayList<>();
        if(statisticalQueryVo.getMold()!=null&&statisticalQueryVo.getMold()==0){
            //只取最高分
            Map<Integer, TestPaper> collect = list3.stream().filter(s -> s.getStudentId() != null).collect(Collectors.groupingBy(TestPaper::getStudentId, Collectors.collectingAndThen(Collectors.maxBy(Comparator.comparing(TestPaper::getScore)), Optional::get)));
            Map<String, TestPaper> collect1 = list3.stream().filter(s -> s.getStudentId() == null).collect(Collectors.groupingBy(TestPaper::getStudentName, Collectors.collectingAndThen(Collectors.maxBy(Comparator.comparing(TestPaper::getScore)), Optional::get)));
            if(CollUtil.isNotEmpty(collect)){
                List<Integer> collect2 = collect.entrySet().stream().map(s -> s.getValue().getId()).collect(Collectors.toList());
                idList.addAll(collect2);
            }
            if(CollUtil.isNotEmpty(collect1)){
                List<Integer> collect2 = collect1.entrySet().stream().map(s -> s.getValue().getId()).collect(Collectors.toList());
                idList.addAll(collect2);
            }
        }else if(statisticalQueryVo.getMold()!=null&&statisticalQueryVo.getMold()==1){
            //取最后一次分数
            Map<Integer, List<TestPaper>> collect = list3.stream().filter(s -> s.getStudentId() != null).collect(Collectors.groupingBy(TestPaper::getStudentId));
            Map<String, List<TestPaper>> collect1 = list3.stream().filter(s -> s.getStudentId() == null).collect(Collectors.groupingBy(TestPaper::getStudentName));
            if(CollUtil.isNotEmpty(collect)){
                List<Integer> collect2 = collect.entrySet().stream().map(s -> s.getValue().get(s.getValue().size()-1).getId()).collect(Collectors.toList());
                idList.addAll(collect2);
            }
            if(CollUtil.isNotEmpty(collect1)){
                List<Integer> collect2 = collect1.entrySet().stream().map(s -> s.getValue().get(s.getValue().size()).getId()).collect(Collectors.toList());
                idList.addAll(collect2);
            }
        }
        if(CollUtil.isNotEmpty(idList)){
            wrapper.clear();
            wrapper.in(TestPaper::getId, idList);
        }
        Page<TestPaper> page = new Page(statisticalQueryVo.getPageNo(),statisticalQueryVo.getPageSize());
        page(page,wrapper);
        List<TestPaper> list = page.getRecords();

        if(CollUtil.isEmpty(list)){
            return null;
        }
        //查询学生姓名
        List<Integer> ids = list.stream().map(TestPaper::getStudentId).collect(Collectors.toList());
        List<RbacUser> rbacUsers = rbacUserService.listByIds(ids);
        Map<Integer,String> map = new HashMap<>();
        if(CollUtil.isNotEmpty(rbacUsers)){
            map = rbacUsers.stream().collect(Collectors.toMap(RbacUser::getId, RbacUser::getName));
        }
        //查询考试名称
        List<Integer> ids2 = list.stream().map(TestPaper::getTestinfoId).collect(Collectors.toList());
        List<Testinfo> testinfos = testinfoService.listByIds(ids2);
        Map<Integer,Testinfo> map2 = new HashMap<>();
        Map<Integer,Double> scoreMap = new HashMap<>();
        if(CollUtil.isNotEmpty(testinfos)){
            map2 = testinfos.stream().collect(Collectors.toMap(Testinfo::getId, Function.identity()));
            scoreMap = testinfos.stream().collect(Collectors.toMap(Testinfo::getId, s->examinationService.countTotalScore(s.getId())));
        }

        Map<Integer, String> finalMap = map;
        Map<Integer, Testinfo> finalMap2 = map2;
        Map<Integer, Double> finalScoreMap = scoreMap;
        List<StatisticalResultVo> collect = list.stream().map(s -> {
            StatisticalResultVo statisticalResultVo = BeanCopyUtils.copyBean(s, StatisticalResultVo.class);
            //1.名称
            statisticalResultVo.setName(finalMap2.get(s.getTestinfoId()) == null ? "" : finalMap2.get(s.getTestinfoId()).getName());
            //2.学生姓名
            statisticalResultVo.setStudentName(finalMap.get(s.getStudentId()) == null ? s.getStudentName() : finalMap.get(s.getStudentId()));
            //3.参加次数
            statisticalResultVo.setPartCount(finalMap2.get(s.getTestinfoId()).getFrequency() - s.getFrequency());
            //3.总分/及格分
            statisticalResultVo.setFraction(finalScoreMap.get(s.getTestinfoId()) + "/" + finalMap2.get(s.getTestinfoId()).getPassScore());
            //5.正确率
            List<BigQuestionTestPaperResultVo> list1 = JSONArray.parseArray(s.getContent(), BigQuestionTestPaperResultVo.class);
            if (CollUtil.isNotEmpty(list1)) {
                List<QuestionTestPaperResultVo> list2 = list1.stream().filter(k -> CollUtil.isNotEmpty(k.getQuestionTestPaperResultVos()))
                        .flatMap(k -> k.getQuestionTestPaperResultVos().stream())
                        .collect(Collectors.toList());
                try{
                    long count1 = list2.stream().filter(k -> !(k.getGetScore().equals(k.getScore()))).count();
                    statisticalResultVo.setAccuracy((double) count1 / list2.size());
                }catch (Exception e){
                    statisticalResultVo.setAccuracy((double) 0);
                }

            } else {
                statisticalResultVo.setAccuracy((double) 0);
            }

            //6.得分率
            statisticalResultVo.setScoringRate(finalScoreMap.get(s.getTestinfoId()) == 0 ? 0 : s.getScore() / finalScoreMap.get(s.getTestinfoId()));
            return statisticalResultVo;
        }).collect(Collectors.toList());
        return new PageResponse<>(collect, page.getTotal(),statisticalQueryVo.getPageNo(),statisticalQueryVo.getPageSize());
    }

    @Override
    public void deleteByTestinfoIds(List<Integer> ids) {
        LambdaQueryWrapper<TestPaper> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(TestPaper::getTestinfoId, ids);
        remove(wrapper);
    }

    @Override
    public void leadingOut(Integer id) {
        //查询考试名称
        Testinfo byId = testinfoService.getById(id);
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        try {
            List<AchievementResultVo> achievementResultVos = this.achievementStatistics(id);
            MyExcelExportUtil.exportExcel(achievementResultVos,AchievementResultVo.class,"成绩统计",byId==null?"成绩统计":byId.getName()+"成绩统计",response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void judgeHasTestCount(Integer testid, String studentName) {
        LambdaQueryWrapper<TestPaper> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TestPaper::getStudentName, studentName).eq(TestPaper::getTestinfoId, testid).orderByDesc(TestPaper::getId).last("limit 1");
        TestPaper one = getOne(wrapper);
        if(one == null){
            return;
        }
        // 没有考试次数
        if(one.getFrequency() == 0){
            AdminResultEnum.NO_TIMES.assertIsTrue(false);
        }
    }

    @Override
    public void judgeHasTestCountByStudentid(Integer testid) {
        LambdaQueryWrapper<TestPaper> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TestPaper::getStudentId, SecurityUtils.getUserId())
                .eq(TestPaper::getTestinfoId, testid).orderByDesc(TestPaper::getId).last("limit 1");
        TestPaper one = getOne(wrapper);
        if(one == null){
            return;
        }
        // 没有考试次数
        if(one.getFrequency() == 0){
            AdminResultEnum.NO_TIMES.assertIsTrue(false);
        }
    }
}
